package com.nkdx.ws.config;

import com.alibaba.fastjson.JSONObject;

import com.nkdx.ws.enums.CodeEnum;
import com.nkdx.ws.exception.DataException;
import com.nkdx.ws.response.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 *   全局异常处理器
 * @author ZZW
 * @date 2021/12/23 6:45 下午
 * @param
 * @return
 */
@RestControllerAdvice
public class SyhdExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(SyhdExceptionHandler.class);
    
    @ExceptionHandler(DataException.class)
    public ApiResponse dataException(DataException e){
        logger.info(e.getMsg());
        return new ApiResponse().error(e.getCode(), e.getMsg(), new JSONObject());
    }
    
    @ExceptionHandler(Exception.class)
    public ApiResponse exception(Exception e){
        e.printStackTrace();
        logger.error(e.getMessage(),e);
        return new ApiResponse().error(CodeEnum.COM_UNKNOW_ERROR,new JSONObject());
    }
}
